package com.qianfeng.vhly.youshare;

import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import com.qianfeng.vhly.youshare.adapters.CommonImagePagerAdapter;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

public class MainActivity extends FragmentActivity implements AdapterView.OnItemClickListener {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // 去掉ActionBar 图标，标题居中

        ActionBar actionBar = getActionBar();

        if (actionBar != null) {
            actionBar.setIcon(new ColorDrawable(Color.TRANSPARENT));

            // ActionBar 标题居中
            // 设置一个默认标题
            actionBar.setDisplayShowCustomEnabled(true);
            actionBar.setCustomView(R.layout.actionbar_main_title);

        }


        ViewPager pager = (ViewPager) findViewById(R.id.main_poster);
        if (pager != null) {
            // TODO 设置Adapter

            LinkedList<Integer> imageIds = new LinkedList<Integer>();

            imageIds.add(R.drawable.main_image1);
            imageIds.add(R.drawable.main_image2);
            imageIds.add(R.drawable.main_image3);
            imageIds.add(R.drawable.main_image4);
            imageIds.add(R.drawable.main_image5);

            CommonImagePagerAdapter adapter = new CommonImagePagerAdapter(this, imageIds);

            pager.setAdapter(adapter);

        }

        // TODO 九宫格

        GridView gridView = (GridView) findViewById(R.id.main_entry);
        if (gridView != null) {

            LinkedList<Map<String, ?>> data = new LinkedList<Map<String, ?>>();

            HashMap<String, Object> map = new HashMap<String, Object>();

            map.put("bg", R.drawable.ico_yingshi_bg);
            map.put("icon", R.drawable.ico_yingshi);
            map.put("title", getString(R.string.main_video));
            data.add(map);

            map = new HashMap<String, Object>();
            map.put("bg", R.drawable.ico_shangcheng_bg);
            map.put("icon", R.drawable.ico_shangcheng);
            map.put("title", getString(R.string.main_store));
            data.add(map);

            map = new HashMap<String, Object>();
            map.put("bg", R.drawable.ico_xianchang_bg);
            map.put("icon", R.drawable.ico_xianchang);
            map.put("title", getString(R.string.main_room));
            data.add(map);

            map = new HashMap<String, Object>();
            map.put("bg", R.drawable.ico_shejiao_bg);
            map.put("icon", R.drawable.ico_shejiao_bg);
            map.put("title", getString(R.string.main_sns));
            data.add(map);

            map = new HashMap<String, Object>();
            map.put("bg", R.drawable.ico_user_bg);
            map.put("icon", R.drawable.ico_user);
            map.put("title", getString(R.string.main_personal));
            data.add(map);

            map = new HashMap<String, Object>();
            map.put("bg", R.drawable.ico_setting_bg);
            map.put("icon", R.drawable.ico_setting);
            map.put("title", getString(R.string.main_settings));
            data.add(map);


            SimpleAdapter adapter = new SimpleAdapter(
                    this,
                    data,
                    R.layout.item_main_enter_point,
                    new String[]{
                            "bg",
                            "icon",
                            "title"
                    },
                    new int[]{
                            R.id.item_enter_point_bg,
                            R.id.item_enter_point_icon,
                            R.id.item_enter_point_title
                    }
            );

            gridView.setAdapter(adapter);

            // 主界面入口进行点击的设置

            gridView.setOnItemClickListener(this);

        }


    }

    /**
     * 加载菜单
     *
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    /**
     * 上一次点击Back按钮的时间
     */
    private long lastBackTime = 0;

    /**
     * 当前Activity显示的时候，点击屏幕上面返回键的事件处理方法
     */
    @Override
    public void onBackPressed() {

        // 1. 获取当前的毫秒数
        long ct = System.currentTimeMillis();
        // 2. 和上一次点击返回键的时间进行比较，相差多少秒就退出

        if (ct - lastBackTime <= 2000) {
            // TODO 结束程序
            // 利用 super.onBackPressed() 让Activity 自己处理返回键
            // 或者用 finish() 结束。

            finish();

            // 也可以通过 System.exit(0) 这个方法，终止整个应用程序进程
            // 进程终止，类似 Service 之类的组件将不在继续运行

        } else {
            lastBackTime = ct;
            Toast.makeText(this, "再次点击退出", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = null;

        switch (position) {
            case 1:   // 跳转商城
                intent = new Intent(this, StoreActivity.class);
                break;
            case 3:
                intent = new Intent(this, WebViewDemoActivity.class);
                break;
        }

        if (intent != null) {
            startActivity(intent);
        }
    }
}
